<!--  -->
<template>
  <div class="star" :class="starType">
      <span class="star-item" v-for="(itemClass,i) in itemClasses"
      :key="i" :class="[itemClass,{ marginR:mr }]"></span>
  </div>
</template>
<script>
const LENGTH = 5;
const CLS_ON = "on";
const CLS_HALF = "half";
const CLS_OFF = "off";
export default {
  data() {
    return {};
  },
  props: ["size", "score", "mr"],
//   mounted() {
//     console.log(this.score);
//   },
//   updated() {
//     console.log(this.score);
//   },
  computed: {
    starType() {
      return "star-" + this.size;
    },
    itemClasses() {
      if (this.score === undefined) {
        return;
      }
      let result = [];
      // 向下取0.5
      let score = Math.floor(this.score * 2) / 2;
      // 判断是否有0.5的情况 即半星
      let hasDecimal = score % 1 !== 0;
      // 求整数是多少
      let integer = Math.floor(score);
      // 向数组放入整星的
      for (let i = 0; i < integer; i++) {
        result.push(CLS_ON);
      }
      // 向数组放入半星的
      if (hasDecimal) {
        result.push(CLS_HALF);
      }
      // 向数组补充无星的
      while (result.length < LENGTH) {
        result.push(CLS_OFF);
      }
      //   console.log(integer);
      //   console.log(result);
      //   console.log(this.score);
      //   console.log(this.size);
      return result;
    }
  }
//   watch: {
//     itemClasses: function(newval, oldval) {
//       console.log(newval + "----" + oldval);
//     }
//   }
};
</script>
<style lang='less' scoped>
@import url("../../common/css/mixin.less");
.star {
  display: inline-block;
  font-size: 0;
  vertical-align: middle;
  &.star-48 {
    .star-item {
      display: inline-block;
      width: 20px;
      height: 20px;
      margin-right: 22px;
      background-size: 20px 20px;
      &.marginR{
        margin-right: 2px;
      }
      &:last-child {
        margin-right: 0;
      }
      &.on {
        .bg-image("star48_on");
      }
      &.half {
        .bg-image("star48_half");
      }
      &.off {
        .bg-image("star48_off");
      }
    }
  }
  &.star-36 {
    .star-item {
      display: inline-block;
      width: 15px;
      height: 15px;
      margin-right: 16px;
      background-size: 15px 15px;
      &.marginR{
        margin-right: 2px;
      }
      &:last-child {
        margin-right: 0;
      }
      &.on {
        .bg-image("star36_on");
      }
      &.half {
        .bg-image("star36_half");
      }
      &.off {
        .bg-image("star36_off");
      }
    }
  }
  &.star-24 {
    .star-item {
      display: inline-block;
      width: 10px;
      height: 10px;
      margin-right: 11px;
      background-size: 10px 10px;
      &.marginR{
        margin-right: 2px;
      }
      &:last-child {
        margin-right: 0;
      }
      &.on {
        .bg-image("star24_on");
      }
      &.half {
        .bg-image("star24_half");
      }
      &.off {
        .bg-image("star24_off");
      }
    }
  }
}
</style>